
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"

unsigned int	uiRANDOM_PlayCount;
unsigned long	ulRANDOM_Calc_Count;
unsigned int	uiRANDOM_OffsetNo;
unsigned int	uiRANDOM_TotalCount;
unsigned char	ucRANDOM_MainPrimeNo;
unsigned char	ucRANDOM_SubPrimeNo;
unsigned int	uiRANDOM_Init[4];
unsigned int	uiRANDOM_Seed;
unsigned int	uiRANDOM_Step[4];
unsigned char	ucRANDOM_BaseArray[3];

unsigned long const uiPrime_NumberTable[] = 
{
	2,3,5,7,11,17,23,37,59,91,139,
	211,317,491,701,997,1499,2003,
	3001,4507,7001,10007,15013,21851,
	33013,47017,65537
};

unsigned short RANDOM_GetNextPlayFileNo(void)
{
	unsigned char Base_Sel;
	unsigned long Temp1 = 0;
	unsigned long Temp2 = 0;
	unsigned long Temp3 = 0;
	unsigned long Temp4 = 0;
	unsigned short usNextNo;

	if  (uiRANDOM_TotalCount  == 0)
	{
		return(usNextNo);
	}
	do{
		Base_Sel = ucRANDOM_BaseArray[ulRANDOM_Calc_Count % 3];

		if  (ulRANDOM_Calc_Count < 3)
		{
			uiRANDOM_Init[Base_Sel] = uiRANDOM_Seed % uiPrime_NumberTable[ucRANDOM_SubPrimeNo];
			uiRANDOM_Step[Base_Sel] = uiRANDOM_Seed % (uiPrime_NumberTable[ucRANDOM_SubPrimeNo] - 1) + 1;
		}

		Temp1 = uiRANDOM_Init[Base_Sel] + uiRANDOM_Step[Base_Sel] * (ulRANDOM_Calc_Count / 3);
		Temp2 = 3 * Temp1 + Base_Sel;
		Temp3 = Temp2 % (uiPrime_NumberTable[ucRANDOM_SubPrimeNo] * 3);
		if  (Temp3 >= uiPrime_NumberTable[ucRANDOM_MainPrimeNo])
		{
			Temp4 = uiRANDOM_TotalCount;
		}
		else
		{
			Temp4 = (unsigned long)(uiRANDOM_Init[3] + Temp3 * uiRANDOM_Step[3]) % uiPrime_NumberTable[ucRANDOM_MainPrimeNo];
		}

		if  (Temp4 < uiRANDOM_TotalCount)
		{
			++uiRANDOM_PlayCount;
		}
		++ulRANDOM_Calc_Count;
	}while(Temp4 >= uiRANDOM_TotalCount);

	usNextNo = Temp4 + uiRANDOM_OffsetNo;	// ???

	LOG_RDM("RANDOM_GetNextPlayFileNo:%d",usNextNo);
	return(usNextNo);
}

void RANDOM_CountRandomSeed(void)
{
	++uiRANDOM_Seed;
}

void RANDOM_MakeBaseArray(void)
{
	unsigned char Init;
	unsigned char Step;

	Init = uiRANDOM_Seed % 3;
	Step = uiRANDOM_Seed % 2 + 1;

	ucRANDOM_BaseArray[0] = Init;
	ucRANDOM_BaseArray[1] = (Init + Step) % 3;
	ucRANDOM_BaseArray[2] = (Init + Step * 2) % 3;
}

void RANDOM_SetRandom(unsigned short usTotalFile, unsigned short usFirstFile)
{
	unsigned int i;
	unsigned int PrameCheckSub;
	unsigned long PrameCheckMain;
	unsigned int Temp;

	LOG_RDM("RANDOM_SetRandom-T:%d F:%d",usTotalFile,usFirstFile);
	uiRANDOM_TotalCount = usTotalFile;
	uiRANDOM_OffsetNo = usFirstFile;

	PrameCheckMain = uiRANDOM_TotalCount;
	for(i = 0; uiPrime_NumberTable[i] < PrameCheckMain; ++i)
	{
	}
	ucRANDOM_MainPrimeNo = i;

	PrameCheckSub = (uiPrime_NumberTable[ucRANDOM_MainPrimeNo] + 2) / 3;	
	for(i = 0; uiPrime_NumberTable[i] < PrameCheckSub; ++i)
	{
	}
	ucRANDOM_SubPrimeNo = i;

	Temp = uiPrime_NumberTable[ucRANDOM_MainPrimeNo];
	uiRANDOM_Init[3] = uiRANDOM_Seed % Temp;
	if  (Temp > 1000)
	{
		Temp = 1000;
	}
	uiRANDOM_Step[3] = uiRANDOM_Seed % (Temp - 1) + 1;

	RANDOM_MakeBaseArray();
	
	uiRANDOM_PlayCount = 0;
	ulRANDOM_Calc_Count = 0;
}


